package com.Smart.smartscals.Utils;

/***
 * huangbin  倒计时的类，单位为秒，使用方法：初始化到时候的时间，启动调用startCountDown();如果要中途要停止任务调用
 * stopCountDown();
 */
import android.os.Handler;

public abstract class CountDownTimer  {
	
	/**倒计时的时间，单位为秒*/
	private int countTime;
	/**/
	private 	Handler handler ;
	private Runnable runnable;
	protected int delay = 0;
	/**
	 * 在倒计时到了要处理的事物,实现本接口;
	 */
	public	abstract void startMyTask();
	
	public void setDelay(int delay){
		this.delay = delay;
	}
	public int getCurrentCountTime(){
		
		return countTime;
	}


	
	public void SetCountTime(int countTime) {
		this.countTime = countTime;
	}



	public CountDownTimer() {		

	}

	public void reStart(){
		handler.postDelayed(runnable, delay);
		startMyTask();
	}

	public void startCountDown(){
		handler = new Handler(); 
		runnable = new Runnable() {  
	        @Override  
	        public void run() {  
	        	if(countTime > 0){
	        		countTime --;
	        	}	
	        	
	        	startMyTask();
	        	if(countTime == 0)
	        		handler.removeCallbacks(this);
	        	else
	        		 handler.postDelayed(runnable, delay);
	        }  
	    };  
	    handler.postDelayed(runnable, delay);
	    startMyTask();
		
	}
	/**
	 * 启动定时器
	 * @param countTime 延时的次数
	 * @param timeDelay 每次延时的时间
	 */
	public void startCountDown(int countTime,int timeDelay){
		this.countTime = countTime;
		this.delay = timeDelay;
		startCountDown();
	}
	/**
	 * 停止计时任务;
	 */
	public void stopCountDown(){
		if(handler != null && runnable != null){
			handler.removeCallbacks(runnable);

		}
	}
	
	

}
